//
//  FlightContactController.m
//  ELife
//
//  Created by xiaozhi on 11-7-15.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "FlightContactController.h"
#import "UIViewUtil.h"

@implementation FlightContactController
@synthesize nameTextField, phoneTextField;


// set contact model
- (void)setFlightContactModel:(FlightContactModel *)contactModel target:(id)target backAction:(SEL)backAction;
{
    [_contactModel release];
    _contactModel = [contactModel retain];
    
    [_target release];
    _target = [target retain];
    
    _backAction = backAction;
}

- (void)dealloc
{
    [_target release];
    [nameTextField release];
    [phoneTextField release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIViewUtil getCommonBg];
    [UIViewUtil viewRoundFrame:[self.view viewWithTag:1]];
    // Do any additional setup after loading the view from its nib.
    
    [self setTitle:@"编辑联系人"];
    if (_contactModel != nil) {
        [nameTextField setText:_contactModel.name];
        [phoneTextField setText:_contactModel.phone];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)save
{
    if ([[nameTextField text] length] == 0
        || [[phoneTextField text] length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"请您将信息填充完整" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
        return;
    }

    if (_contactModel == nil) {
        _contactModel = [[FlightContactModel alloc] init];
    }
    _contactModel.name = nameTextField.text;
    _contactModel.phone = phoneTextField.text;
    
    [_target performSelector:_backAction withObject:_contactModel];
    [_contactModel release];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)inputFinished
{
    [nameTextField resignFirstResponder];
    [phoneTextField resignFirstResponder];
}

@end
